package com.ycu.jk16.mall.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ycu.jk16.mall.dao.ShoppingCartDao;
import com.ycu.jk16.mall.domain.ShoppingCart;
import com.ycu.jk16.mall.service.ShoppingCartService;

@Service("shoppingCartService")
public class ShoppingCartServiceImpl implements ShoppingCartService {

	@Autowired
	private ShoppingCartDao mShoppingCartDao;

	/*
	 * 根据登陆用户的Id 选择用户的购物车
	 */
	public List<ShoppingCart> selectShoppingCartByUserId(Integer userId) {
		return mShoppingCartDao.selectShoppingCartByUserId(userId);
	}

	/*
	 * 根据登陆用户的购物车列表 返回价格
	 */
	public BigDecimal getPrice(List<ShoppingCart> shoppingCartList) {
		double totleMoney = 0.0;
		int count;

		for (ShoppingCart shoppingCart : shoppingCartList) {
			count = shoppingCart.getQuantity();
			totleMoney += shoppingCart.getProduct().getPrice().doubleValue() * count;
		}
		return BigDecimal.valueOf(totleMoney);
	}

	/*
	 * 增加用户的购物车列表中的一种商品记录
	 */
	public void addShoppingCart(Integer userId, Integer productId, Integer quantity) {
		java.sql.Date createTime = new java.sql.Date(new Date().getTime());
		ShoppingCart shoppingCart = selectShoppingCartByUserIdWithProductId(userId, productId);
		if (shoppingCart != null) {
			Integer count = shoppingCart.getQuantity() + quantity;
			updateShoppingCartOfQuantity(userId, productId, count); // 如果存在
																	// 用户id和商品id同时相同的
																	// 直接在原先的基础上增加数量即可
		} else {
			//// 如果存在 用户id和商品id 不同时相同的 直接创建一条新的数据
			mShoppingCartDao.addShoppingCart(userId, productId, quantity, createTime);
		}
	}

	/*
	 * 根据用户的Id和商品Id在该用户的购物车列表中的一种商品记录
	 */
	public ShoppingCart selectShoppingCartByUserIdWithProductId(Integer userId, Integer productId) {
		return mShoppingCartDao.selectShoppingCartByUserIdWithProductId(userId, productId);
	}

	/*
	 * 根据用户的Id和商品Id更新该用户在购物车列表中的该商品数量
	 */
	public void updateShoppingCartOfQuantity(Integer userId, Integer productId, Integer count) {
		ShoppingCart shoppingCart = selectShoppingCartByUserIdWithProductId(userId, productId);
		shoppingCart.setQuantity(count);
		mShoppingCartDao.updateShoppingCartOfQuantity(shoppingCart);

	}

	// 删除购物车中的一条记录
	public void deleteShoppingCart(Integer userId, Integer productId) {
		ShoppingCart shoppingCart = selectShoppingCartByUserIdWithProductId(userId, productId);
		mShoppingCartDao.deleteShoppingCart(shoppingCart.getId());
	}

	// 返回该用户购物车中所有的商品数量
	public Integer allProductOfCart(List<ShoppingCart> shoppingCartList) {
		Integer count = 0;
		for (ShoppingCart shoppingCart : shoppingCartList) {
			count += shoppingCart.getQuantity();
		}
		return count;
	}

	// 将得到的购物车生成对应的json数据插入到map中
	public void insertCartDataToMap(Map<String, Object> dataInfo, List<ShoppingCart> shoppingCartList) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		BigDecimal productTotalPrice;
		for (ShoppingCart cart : shoppingCartList) {
			Map<String, Object> datainfo = new HashMap<String, Object>();

			productTotalPrice = cart.getProduct().getPrice().multiply(BigDecimal.valueOf(cart.getQuantity()));

			datainfo.put("id", cart.getId());
			datainfo.put("userId", cart.getUser().getId());
			datainfo.put("productId", cart.getProduct().getId());
			datainfo.put("quantity", cart.getQuantity());
			datainfo.put("productName", cart.getProduct().getName());
			datainfo.put("productSubtitle", cart.getProduct().getSubtitle());
			datainfo.put("productMainImage", cart.getProduct().getMainImage());
			datainfo.put("productPrice", cart.getProduct().getPrice());
			datainfo.put("productStatus", cart.getProduct().getStatus());
			datainfo.put("productTotalPrice", productTotalPrice);
			datainfo.put("productStock", cart.getProduct().getStock());
			datainfo.put("productChecked", 1);
			datainfo.put("limitQuantity", "LIMIT_NUM_SUCCESS");

			list.add(datainfo);
		}
		dataInfo.put("cartProductVoList", list);
	}
}
